UNPKG

5.37 kBJavaScriptView Raw
1'use strict';
2
3var IFNode = require('../');
4var Path = require('path');
5var Should = require('should');
6var SuperTest = require('supertest');
7
8var app = IFNode({
9 project_folder: Path.resolve(__dirname, '../examples/controllers'),
10 alias: 'cntllrs'
11}).load();
12
13var app_map = IFNode({
14 project_folder: Path.resolve(__dirname, '../examples/controller_map'),
15 alias: 'cntllrs_map'
16}).load();
17
18describe('Controllers', function() {
19 describe('app.Controller(options?: Object)', function() {
20 it('default options', function() {
21 app.controllers.main.root.should.be.equal('/');
22 app.controllers.main.name.should.be.equal('main');
23
24 app.controllers['api/~'].root.should.be.equal('/api/');
25 app.controllers['api/~'].name.should.be.equal('api/~');
26
27 app.controllers['api/v1/user'].root.should.be.equal('/api/v1/user/');
28 app.controllers['api/v1/user'].name.should.be.equal('api/v1/user');
29 });
30 });
31
32 describe('Controller instance', function() {
33 describe('.param(name: string, expression: function)', function() {
34 it('should get correct params', function() {
35 (function() {
36 app.controllers.main.param('abc', /test/);
37 }).should.throw();
38 (function() {
39 app.controllers.main.param(/bad-name/, function() {});
40 }).should.throw();
41 });
42
43 it('should create param checker', function() {
44 app.controllers.main.param('param-checker', function() {
45 });
46 })
47 });
48
49 describe('options', function() {
50 it('.before', function(done) {
51 SuperTest(app.listener)
52 .get('/check_before')
53 .expect({ with_user: 'no' }, done);
54 });
55
56 it('permanent', function(done) {
57 SuperTest(app.listener)
58 .get('/check_permanent_options')
59 .expect({ permanent: 'yes' }, done);
60 });
61 it('custom', function(done) {
62 SuperTest(app.listener)
63 .get('/check_custom_options')
64 .expect({ custom: 'yes' }, done);
65 });
66
67 describe('.map', function() {
68 it('get /', function(done) {
69 SuperTest(app_map.listener)
70 .get('/')
71 .expect('simple route', done);
72 });
73 it('get /:param', function(done) {
74 SuperTest(app_map.listener)
75 .get('/1')
76 .expect({ id: 1, name: 'ilfroloff' }, done);
77 });
78 it('post /', function(done) {
79 SuperTest(app_map.listener)
80 .post('/')
81 .expect({ permanent: 'yes' }, done);
82 });
83 it('put /:id', function(done) {
84 SuperTest(app_map.listener)
85 .put('/1')
86 .expect('updated', done);
87 });
88 it('delete /:id', function(done) {
89 SuperTest(app_map.listener)
90 .delete('/1')
91 .expect({ custom: 'yes' }, done);
92 });
93 });
94 });
95
96 describe('.method(method: string|Array, route?: string, options?: Object, handler: function)', function() {
97 describe('map of methods', function() {
98 it('default', function(done) {
99 SuperTest(app.listener)
100 .get('/')
101 .expect({ default: true }, done);
102 });
103
104 it('get', function(done) {
105 SuperTest(app.listener)
106 .get('/10')
107 .expect({ id: 10 }, done);
108 });
109 it('post', function(done) {
110 SuperTest(app.listener)
111 .post('/11')
112 .expect({ id: 11 }, done);
113 });
114 it('put', function(done) {
115 SuperTest(app.listener)
116 .put('/a')
117 .expect({ id: 0 }, done);
118 });
119 it('delete', function(done) {
120 SuperTest(app.listener)
121 .delete('/method-delete')
122 .expect('works', done);
123 });
124 it('del', function(done) {
125 SuperTest(app.listener)
126 .delete('/method-del')
127 .expect('works', done);
128 });
129 });
130
131 it('should jump to next controller', function(done) {
132 var url = '/api/jump/to/next';
133
134 SuperTest(app.listener)
135 .get(url)
136 .expect({
137 echo: url
138 }, done)
139 })
140 });
141
142 describe('.error(handler: function)', function() {
143 it('fire', function(done) {
144 SuperTest(app.listener)
145 .get('/error/fire')
146 .expect({ error: 'fire' }, done);
147 });
148 });
149 });
150});